﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Collections;
using System.Drawing;
using System.Windows.Forms;

namespace TouchUmlUI
{
    public class ZapisOdczyt
    {
        private ZapisOdczyt()
        {
        }
        public static void Zapisz(OurTabbedConrol tc, String nazwaPliku)
        {
            List<Diagram.ModelDiagramu> listaDiagramow = new List<Diagram.ModelDiagramu>();
            foreach (OurTab ot in tc.Tabs)
            {
                ot.updateElementPositions();
                (((OurCanvas)ot.canvas).model).Nazwa = ot.getTitle();
                listaDiagramow.Add(((OurCanvas)ot.canvas).model);                
                listaDiagramow[listaDiagramow.Count - 1].Rozmiar = new Size((int)ot.canvas.ActualWidth, (int)ot.canvas.ActualHeight);
                listaDiagramow[listaDiagramow.Count - 1].stroki = DiagramStrokes.stworzStrolki(ot.canvas.Strokes);
            }
            BinaryFormatter bf = new BinaryFormatter();
            FileStream fs = new FileStream(nazwaPliku, FileMode.Create);
            bf.Serialize(fs, listaDiagramow);
            fs.Close();
        }

        public class AssociationItem
        {
            OurCanvas canvas;
            UMLType type;
            Diagram.ElementNaDiagramie end;

            public OurCanvas getCanvas()
            {
                return canvas;
            }

            public UMLType getUmlType()
            {
                return type;
            }

            public Diagram.ElementNaDiagramie getElement()
            {
                return end;
            }

            public AssociationItem(OurCanvas a_canvas, UMLType a_type, Diagram.ElementNaDiagramie a_end)
            {
                canvas = a_canvas;
                type = a_type;
                end = a_end;
            }
        }

        public static void WczytajListe(OurTabbedConrol tabbedControl, String nazwaPliku)
        {                        
            List<Diagram.ModelDiagramu> lista;
            BinaryFormatter bf = new BinaryFormatter();
            FileStream fs = new FileStream(nazwaPliku, FileMode.Open);
            lista = (List<Diagram.ModelDiagramu>)bf.Deserialize(fs);
            fs.Close();
            
            //
            tabbedControl.Clear();
            foreach (Diagram.ModelDiagramu md in lista)
            {                
                List<AssociationItem> associationItems = new List<AssociationItem>();
                tabbedControl.addTab(md.Nazwa, false);
                OurTab tab = tabbedControl.Tabs[lista.IndexOf(md)];
                tab.canvas.Strokes.Clear();
                tab.canvas.Strokes = DiagramStrokes.pobierzStrolki(md.stroki);
                //---
                double nw =  md.Rozmiar.Width;
                double nh =  md.Rozmiar.Height;
                if (nw < SignalR.windowParent.ActualWidth) 
                    nw = SignalR.windowParent.ActualWidth;
                if (nh < SignalR.windowParent.ActualHeight) 
                    nh = SignalR.windowParent.ActualHeight;

                tab.canvas.Width =nw;
                tab.canvas.Height =nh;                

                //----
                foreach (Diagram.ElementNaDiagramie el in md.Elementy)
                {
                    UMLType umlType = UMLType.BYHAND;
                    if (el.JestPrzypadkiemUzycia) umlType = UMLType.ACTION;
                    if (el.JestAktorem) umlType = UMLType.ACTOR;
                    if (el.JestAsocjacja) umlType = UMLType.ASSOCIATION;
                    if (el.JestKlasa) umlType = UMLType.CLASS;
                    if (el.JestInterfejsem) umlType = UMLType.INTERFACE;
                    if (el.JestNotatka) umlType = UMLType.NOTICE;
                    //
                    if (umlType == UMLType.ASSOCIATION)
                    {
                        associationItems.Add(new AssociationItem(tab.canvas as OurCanvas,umlType,el));
                    }
                    else
                    {
                        GUML guml = SignalR.utworzObiektZElementuDiagramu(tab.canvas as OurCanvas, umlType, el);
                        ((Diagram.ElementNaDiagramie)guml.obj_uml).polozenie = el.polozenie;
                        ((Diagram.ElementNaDiagramie)guml.obj_uml).rozmiar = el.rozmiar;
                    }
                }

                foreach (AssociationItem aItem in associationItems)
                {
                    SignalR.utworzObiektZElementuDiagramu(aItem.getCanvas(), aItem.getUmlType(), aItem.getElement());
                }
                associationItems.Clear();
                associationItems = null;

            }
            tabbedControl.Select(0);
            tabbedControl.Refresh();
            SignalR.windowParent.treeModelView.setTabbedControl(tabbedControl);
            SignalR.windowParent.treeModelView.Refresh();
            Directory.SetCurrentDirectory(SignalR.windowParent.curDir);
            if (SignalR.windowParent.Hint.Opacity == 1.0f)
                SignalR.windowParent.showAllHints(true);
            else
                SignalR.windowParent.showAllHints(false);
            //FileStream fs2 = new FileStream("plik.txt", FileMode.Create);
            //Diagram.ModelDiagramu.model.GenerujKodCSharp(fs2);
        }        
    }
}
